package szcg.scheduled.service.impl.ydcg;

import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import szcg.framework.library.base.PostServiceImpl;
import szcg.scheduled.service.service.ydcg.TaskLogService;

import java.time.LocalDate;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;


@Service
public class TaskLogServiceImpl extends PostServiceImpl implements TaskLogService {


    @Qualifier("MainSqlSessionFactory")
    private final SqlSessionFactory sqlSessionFactory;

    public TaskLogServiceImpl(SqlSessionFactory sqlSessionFactory) {
        this.sqlSessionFactory = sqlSessionFactory;
    }

    @Override
    public void addTaskLog(String taskId, String executionStartTime, String executionEndTime, String executionDuration) {
        try (SqlSession session = sqlSessionFactory.openSession(true)) {
//            TaskLogMapper mapper = session.getMapper(TaskLogMapper.class);
//            HashMap<String, String> params = new HashMap<>();
//            params.put("taskId", taskId);
//            params.put("executionStartTime", executionStartTime);
//            params.put("executionEndTime", executionEndTime);
//            params.put("executionDuration", executionDuration);
//            mapper.addTaskLog(params);
        }
    }

    @Override
    public String queryLog(String pageSize, String pageIndex) {
        //参数校验
        if (pageSize.isEmpty() || pageIndex.isEmpty()) {
            return generateFail("参数异常");
        }
        int intPageSize = Integer.parseInt(pageSize);//每页展示多少条数据
        int intPageIndex = Integer.parseInt(pageIndex);//第几页
        if (intPageSize < 1 || intPageSize > 200) {
            return generateFail("参数异常:pageSize");
        }
        try (SqlSession session = sqlSessionFactory.openSession(true)) {
//            TaskLogMapper mapper = session.getMapper(TaskLogMapper.class);
//            int totalCount = mapper.queryCount();//总行数
//            //根据总行数和每页展示的行数计算总页数
//            int totalPages = (totalCount + intPageSize - 1) / intPageSize;
//            if (totalPages == 0) {
//                //封装响应体
//                HashMap<String, Object> resMap = new HashMap<>();
//                resMap.put("totalCount", totalCount);
//                resMap.put("taskLogList", new ArrayList<TaskLogBean>());
//                return generateSucceed(resMap);
//            }
//            if (intPageIndex < 1 || intPageIndex > totalPages) {
//                return generateFail("参数异常:pageIndex");
//            }
//            //查询开始
//            HashMap<String, Object> params = new HashMap<>();
//            params.put("limit", intPageSize);//limit和offset参数必须是int类型
//            int offset = intPageSize * (intPageIndex - 1);//计算偏移量
//            params.put("offset", offset);
//            List<TaskLogBean> taskLogList = mapper.queryTaskLog(params);
//            //封装响应体
//            HashMap<String, Object> resMap = new HashMap<>();
//            resMap.put("totalCount", totalCount);
//            resMap.put("taskLogList", taskLogList);
//            return generateSucceed(resMap);
        }

        return "";
    }

    @Override
    public String deleteLog(String deleteAction) {
        LocalDate now = LocalDate.now(); // 获取当前日期
        LocalDate previous;
        switch (deleteAction) {
            case "3d":
                //3天前
                previous = now.minusDays(3);
                break;

            case "1y":
                //个月前
                previous = now.minusMonths(3);
                break;

            case "3y":
                //3个月前
                previous = now.minusMonths(6);
                break;

            case "12y":
                //全部
                previous = now.minusMonths(12);
                break;

            default:
                previous = null;
                break;
        }
        //previous格式xxxx-xx-xx
        try (SqlSession session = sqlSessionFactory.openSession(true)) {
            if (previous == null) {
                return generateFail("参数异常");
            }
            String startTime = "1990-01-01 00:00:00";
            String endTime = previous + " 23:59:59";
//            TaskLogMapper mapper = session.getMapper(TaskLogMapper.class);
//            mapper.deleteLog(startTime, endTime);
            return generateSucceed("删除成功");
        }
    }

}
